export const getNumber = (val: any) => {
  if (!val) {
    return 0
  }
  if (typeof val !== 'number') {
    return Number(val)
  }
  return val
}

// 至多保留n位小数
export const myToFixed = (val: number, n: number) => {
  const num = val.toFixed(n)
  return parseFloat(num).toString()
}

/**
 * 生成带有时间戳的随机捕获ID
 * 格式: 时间戳-UUID
 * @returns 捕获ID字符串
 */
export function generateCaptureId(): string {
  // 获取当前时间戳
  const timestamp = Date.now()

  // 生成UUID v4
  const uuid = generateUUID()

  // 组合时间戳和UUID
  return `${timestamp}-${uuid}`
}

/**
 * 生成UUID v4
 * @returns UUID字符串
 */
function generateUUID(): string {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    const r = (Math.random() * 16) | 0
    const v = c === 'x' ? r : (r & 0x3) | 0x8
    return v.toString(16)
  })
}
